package com.sinothk.fc.storeSupplier.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.fc.store.entity.StoreEntity;
import com.sinothk.fc.store.mapper.FcStoreMapper;
import com.sinothk.fc.storeSupplier.entity.StoreSupplierEntity;
import com.sinothk.fc.storeSupplier.entity.StoreSupplierReq;
import com.sinothk.fc.storeSupplier.entity.StoreSupplierVO;
import com.sinothk.fc.storeSupplier.mapper.StoreSupplierMapper;
import com.sinothk.fc.storeSupplier.service.IStoreSupplierService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description: 门店供应商
 * @Author: Liangyt
 * @Date: 2022-10-29
 * @Version: V1.0
 */
@Service
public class StoreSupplierServiceImpl extends ServiceImpl<StoreSupplierMapper, StoreSupplierEntity> implements IStoreSupplierService {

    @Resource
    private StoreSupplierMapper storeSupplierMapper;
    @Resource
    private FcStoreMapper storeMapper;

    @Override
    public boolean supplierAdd(String supplierId) throws Exception {
        if (StringUtils.isBlank(supplierId)) throw new Exception("待添加的供应商id不能为空");
        LoginUser loginUser = getLoginUser();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");

        LambdaQueryWrapper<StoreSupplierEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(StoreSupplierEntity::getStoreId, loginUser.getStoreId())
                .eq(StoreSupplierEntity::getSupplierId, supplierId);
        StoreSupplierEntity storeSupplierDB = getOne(queryWrapper);
        if (storeSupplierDB == null) {
            storeSupplierDB = new StoreSupplierEntity();
            storeSupplierDB.setStoreId(loginUser.getStoreId());
            storeSupplierDB.setSupplierId(supplierId);

            StoreEntity store = storeMapper.selectById(supplierId);

            storeSupplierDB.setStoreLogo(store.getStoreLogo());
            storeSupplierDB.setStoreShortName(store.getStoreShortName());

            storeSupplierDB.setStoreManagerId(store.getStoreManagerId());
            storeSupplierDB.setStoreManagerName(store.getStoreManagerName());
            storeSupplierDB.setStoreManagerPhone(store.getStoreManagerPhone());
            storeSupplierDB.setLocAddress(store.getLocAddress());
            storeSupplierDB.setLocLat(store.getLocLat());
            storeSupplierDB.setLocLng(store.getLocLng());
            storeSupplierDB.setFromWhere(1); // 来源:0手动录入1加入供应商
            save(storeSupplierDB);
            return true;
        } else {
            remove(queryWrapper);
            return false;
        }
    }

    private LoginUser getLoginUser() {
        return (LoginUser) SecurityUtils.getSubject().getPrincipal();
    }

    @Override
    public StoreSupplierEntity getStoreSupplierEntityBySupplierId(String supplierId) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");

        LambdaQueryWrapper<StoreSupplierEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(StoreSupplierEntity::getStoreId, loginUser.getStoreId())
                .eq(StoreSupplierEntity::getSupplierId, supplierId);
        return getOne(queryWrapper);
    }


    @Override
    public IPage<StoreSupplierVO> storeSupplierPage(Page<StoreSupplierEntity> page, StoreSupplierReq req) throws Exception {
        LoginUser loginUser = getLoginUser();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        req.setStoreId(loginUser.getStoreId());
        return storeSupplierMapper.storeSupplierPage(page, req);
    }

    @Override
    public StoreSupplierEntity supplierLocalAdd(StoreSupplierEntity storeSupplier) throws Exception {

        if (StringUtils.isBlank(storeSupplier.getId())) {
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");

            storeSupplier.setStoreId(loginUser.getStoreId());
            storeSupplier.setSupplierId(UUID.fastUUID().toString());
            storeSupplier.setStoreManagerId(UUID.fastUUID().toString());

            storeSupplier.setFromWhere(0); // 来源:0手动录入1加入供应商
            save(storeSupplier);
        }else{
            updateById(storeSupplier);
        }
        return storeSupplier;
    }
}
